<?php

/*
 *  Author: Suwandi Tan ( swndtan@gmail.com )
 *  
 *  License: Apache License 2
 *  Copyright 2012 Suwandi Tan
 *
 *  Indonesia:
 *  Lisensi dibawah lisensi Apache, versi 2 ( "LISENSI" );
 *  Kamu tidak boleh menggunakan berkas ini kecuali tunduk dengan lisensi ini.
 *  Kamu bisa dapatkan salinan dari lisensi ini di
 *    
 *       http://www.apache.org/licenses/LICENSE-2.0
 *    
 *  Jika tidak diperlukan oleh hukum yang berlaku atau disetujui secara tertulis,
 *  piranti lunak dibawah LISENSI ini disebarluaskan "APA ADANYA", TANPA GARANSI 
 *  ATAU SYARAT APAPUN, baik tersirat maupun tersurat.
 *  Lihat lisensi ini lebih lengkap dalam bahasa kamu mengenai perijinan dan
 *  keterbatasan lisensi ini.
 *
 *  English: 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

{
    function num2words( $num, $suffix = '' )
    {
        $ones = array
        (
            "", "satu ", "dua ", "tiga ", "empat ", "lima ",
            "enam ", "tujuh ", "delapan ", "sembilan ", 
            "satu " => "se",
        );
        
        $tens = array
        (
            "sepuluh ", "sebelas ", "dua belas ", "tiga belas ", "empat belas ", "lima belas ",
            "enam belas ", "tujuh belas ", "delapan belas ", "sembilan belas "
        );
        
        $twenties = array
        (
            "", "sepuluh ", "dua puluh ", "tiga puluh ", "empat puluh ", "lima puluh ",
            "enam puluh ", "tujuh puluh ", "delapan puluh ", "sembilan puluh ",
        );
        
        $thousands = array
        (
            "", "ribu ", "juta ", "miliar ", "triliun ", "dwiyar ", "trita ",
            "sektiliun ", "septiliun ", "oktiliun ", "noniliun ", "desiliun ",
            "undesiliun ", "duodesiliun ", "tredesiliun ", "quatordesiliun ",
            "quindesiliun ", "sekdesiliun ", "septendesiliun ", "oktodesiliun ",
            "novemdesiliun ", "vigintiliun ", "centiliun ",
        );
        
        $n3 = array();
        $ns = ( string ) sprintf( '%33.0f', $num );
        
        for( $i=3; $i<34; $i+=3 )
        {
            $r = substr( $ns, -$i );
            $q = strlen( $ns ) - $i;
            
            if( $q < -2 ) break;
            else
            {
                if( $q >= 0 ) $n3 [] = ( float ) substr( $r, 0, 3 );
                elseif( $q >=-1 ) $n3 [] = ( float ) substr( $r, 0, 2 );
                elseif( $q >=-2 ) $n3 [] = ( float ) substr( $r, 0, 1 );
            }
        }
        
        $nw = '';
        foreach( $n3 as $i => $x )
        {
            $d1 = floor( $x % 10 );
            $d2 = floor( ( $x % 100 ) / 10 );
            $d3 = floor( ( $x % 1000 ) / 100 );
            
            if( $x == 0 ) continue;
            else $t = $thousands[ $i ];
            
            if( $d2 == 0 ) $nw = sprintf( '%s%s%s', 
                ( ( isset( $ones[ $ones[$d1] ] ) AND $i<2 )? $ones[ $ones[$d1] ] : $ones[$d1] ),
                $t, $nw 
            );
            
            elseif( $d2 == 1 ) $nw = sprintf( '%s%s%s', $tens[$d1], $t, $nw );
            elseif( $d2 > 1 ) $nw = sprintf( '%s%s%s%s', $twenties[$d2], 
                ( ( isset( $ones[ $ones[$d1] ] ) AND $i<2 )? $ones[ $ones[$d1] ] : $ones[$d1] ),
                $t, $nw 
            );
            
            if( $d3 > 0 ) $nw = sprintf( '%s%s%s', 
                ( ( isset( $ones[ $ones[$d3] ] ) )? $ones[ $ones[$d3] ] : $ones[$d3] ), 
                'ratus ', $nw 
            );
        }
        
        return $nw.$suffix;
    }
}
